package com.yami.shop.otherservice;

import com.google.gson.Gson;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class SendSmsService {
    //AccessKey ID:LTAI5tRucgYT9mTd94n3LRdb
    //AccessKey Secret:KCGQtWzhYuxxThCGhdDIaSus7v032R

    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }
    //https://next.api.aliyun.com/api/Dysmsapi/2017-05-25/SendSms?params={%22RegionId%22:%22cn-hangzhou%22,%22PhoneNumbers%22:%2213462627296%22,%22SignName%22:%22%E9%B9%8F%E8%B6%8A%E8%BD%AF%E4%BB%B6%22,%22TemplateCode%22:%22SMS_465045178%22,%22TemplateParam%22:%22%7B%5C%22name%5C%22%3A%5C%22%E6%99%AF%E6%98%A5%E5%BE%B7%5C%22%2C%5C%22position%5C%22%3A%5C%221%E5%8D%95%E5%85%83%E6%A5%BC%E6%A2%AF%E5%8F%A3%5C%22%2C%5C%22faultDescription%5C%22%3A%5C%22%E7%94%B5%E7%BA%BF%E7%A0%B4%E7%9A%AE%5C%22%7D%22}&tab=DOC&lang=JAVA
    public  void sendSms(String phones, String templateCode, Map templateParam){
        try {

            Gson gson=new Gson();
            String s = gson.toJson(templateParam);
            //输入你自己的aliyun短信accessKeyId和accessKeySecret
            com.aliyun.dysmsapi20170525.Client client = createClient("LTAIXXXXXXXXXXXX", "KCGQXXXXXXXXXXXXXXX");
            com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                    .setPhoneNumbers(phones)
                    .setSignName("鹏越软件")
                    .setTemplateCode(templateCode)
                    .setTemplateParam(s);
            com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();

            com.aliyun.dysmsapi20170525.models.SendSmsResponse resp = client.sendSmsWithOptions(sendSmsRequest, runtime);
            com.aliyun.teaconsole.Client.log(com.aliyun.teautil.Common.toJSONString(resp));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 测试用例
     * @param args
     */
    public static void main(String[] args) {
        Map map=new HashMap();
        map.put("name","景春升");
        map.put("position","南阳市金马特-1栋1单元-1002");
        map.put("faultDescription","普通吸顶灯维修-我是标题--777666-我是详情");


        //SendSmsService sendSmsService=new SendSmsService();
        //sendSmsService.sendSms("13462627296","SMS_465045178",map);
    }
}
